#include "test.hpp"
Date::Date(int year,int month,int day)
{
    _year = year;
    _month = month;
    _day = day;
    cout << "构造函数" << endl;
}
Date& Date::operator+=(int days)
{
    if(days < 0)
    {
        return *this -= -days;
    }
    _day = days + _day;
    while(_day > Monthdays(_year,_month))
    {
        _day -= Monthdays(_year,_month);
        _month++;
        if(_month>12)
        {
            _month = 1;
            _year++;
        }
    }
    return *this;
}
int Monthdays(int year,int Month)
{
    int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(Month == 2 &&((year % 4 == 0 && year % 100 != 0) || year % 400 ==0))
    {
        return arr[Month]+1;
    }
    return arr[Month];
}
void Date::Print()
{
    cout <<_year <<"-"<< _month << "-" << _day <<endl;
}

void Test1 ()
{
    Date dt(2024,4,27);
    dt.Print();
    dt+=30;
    dt.Print();
    dt-=30;
    dt.Print();
    
}
// -=运算符重载
// 先判断是否为负数，如果是负数则调用+=运算符重载
Date& Date::operator-=(int days)
{
    if(days < 0)
    {
        return *this += -days;
    }
    _day -= days;
    while(_day <= 0)
    {
        _month--;
        if(_month < 1)
        {
            _month = 12;
            _year--;
        }
        _day += Monthdays(_year,_month);
    }
    return *this;
}
// 前置++运算符重载
Date& Date::operator++ ()
{
    _day++;
    if(_day > Monthdays(_year,_month))
    {
        _day = 1;
        _month++;
        if(_month > 12)
        {
            _month = 1;
            _year++;
        }
    }
    return *this;
}
// 后置++运算符重载
Date& Date::operator++ (int)
{
    Date temp = *this;
    _day++;
    if(_day > Monthdays(_year,_month))
    {
        _day = 1;
        _month++;
        if(_month > 12)
        {
            _month = 1;
            _year++;
        }
    }
    return temp;
    
}
// ==运算符重载
bool Date::operator==(const Date&d)
{
    if(_year == d._year && _month == d._month && _day == d._day)
    {
        return true;
    }
    return false;
}
// !=运算符重载
bool Date::operator!=(const Date&d)
{
    if(*this == d)
    {
        return false;
    }
    return true;
}
// >运算符重载
bool Date::operator>(const Date&d)
{
    if (_year < d._year)
    {
        return true;
    }
    else if (_year == d._year)
    {
        if (_month < d._month)
        {
            return true;
        }
        else if (_month == d._month)
        {
            return _day < d._day;
        }
    }
    return false;
    
}
bool Date::operator< (const Date&d)
{
    if(*this == d || *this > d)
    {
        return false;
    }
    else
    {
        return true;
    }
}
bool Date::operator<= (const Date&d)
{
    if(*this > d)
    {
        return false;
    }
    else{
        return true;
    }
}
bool Date::operator>= (const Date&d)
{
    if(*this < d)
    {
        return false;
    }
    else{
        true;
    }
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "⽉" << d._day << "⽇" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cout << "请依次输⼊年⽉⽇:>";
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "⽇期⾮法" << endl;
}
return in;
}
bool Date::CheckDate()
{
if (_month < 1 || _month > 12
|| _day < 1 || _day > Monthdays(_year, _month))
{
return false;
}
else
{
return true;
}
}
int main()
{
    Test1 ();
}
